const mixin = {
  data() {
    return {
      resourseClasses: [],
      curTypeIndex: "",
    };
  },
  watch: {
    curTypeIndex(index) {
      if (index == -1) {
        return false;
      }
      let intersectionStudnetIdsList = [];
      let txt = "";
      if (this.form.ccCourseMajorList.length > 1) {
        for (let i = 0; i < this.form.ccCourseMajorList.length; i++) {
          if (index !== i) {
            // 获取 和某个类型的并集 用于剔除
            let intersectionStudnetIds = [];
            intersectionStudnetIds = this.intersection(
              this.form.ccCourseMajorList[i].classIds.map((item) => {
                return parseInt(item);
              }),
              this.form.ccCourseMajorList[index].classIds
            );
            if (intersectionStudnetIds.length > 0) {
              intersectionStudnetIdsList.push({
                list: intersectionStudnetIds,
                index: i,
              });
            }

            // 与所有班级对比 获取班级名称
            let intersectionClassnames = [];
            for (let h = 0; h < intersectionStudnetIds.length; h++) {
              for (let g = 0; g < this.resourseClasses.length; g++) {
                if (
                  intersectionStudnetIds[h] == this.resourseClasses[g].class_id
                ) {
                  intersectionClassnames.push(
                    this.resourseClasses[g].class_name
                  );
                }
              }
            }
            console.log(
              intersectionStudnetIdsList,
              intersectionStudnetIds,
              intersectionClassnames
            );
            if (intersectionStudnetIds.length > 0) {
              txt += `【${intersectionClassnames.join(
                ","
              )}】已在${this.returnType(this.form.ccCourseMajorList[i].type)}(${
                this.form.ccCourseMajorList[i].credit
              }学分)中,`;
            }
            this.curTypeIndex = -1;
          }
        }
        // 如果没有txt则代表没有重复
        console.log(intersectionStudnetIdsList[0].list);
        if (txt) {
          this.$confirm(txt + "是否调整到该"+this.$courseName?this.$courseName:'课程'+"类型中", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnClickModal: false,
            type: "warning",
          })
            .then(() => {
              for (let i = 0; i < intersectionStudnetIdsList.length; i++) {
                // 去除上一个卡片的 交集
                let arr = JSON.parse(
                  JSON.stringify(
                    this.form.ccCourseMajorList[
                      intersectionStudnetIdsList[i].index
                    ].classIds
                  )
                );
                this.form.ccCourseMajorList[
                  intersectionStudnetIdsList[i].index
                ].classIds = this.difference(
                  intersectionStudnetIdsList[i].list,
                  arr.map((item) => {
                    return parseInt(item);
                  })
                );
              }
              this.curTypeIndex = -1;
            })
            .catch(() => {
              for (let i = 0; i < intersectionStudnetIdsList.length; i++) {
                // 去除当前卡片的 交集
                let arr = JSON.parse(
                  JSON.stringify(this.form.ccCourseMajorList[index].classIds)
                );
                this.form.ccCourseMajorList[index].classIds = this.difference(
                  intersectionStudnetIdsList[i].list,
                  arr
                );
              }
              this.curTypeIndex = -1;
            });
        }
      }
    },
  },
  methods: {
    // 返回类型
    returnType(e) {
      switch (e) {
        case 0:
          return "必修";
        case 1:
          return "选修";
        case 2:
          return "考证";
        case 3:
          return "工学";
      }
    },
    // 数组取交集方法
    intersection(arr1, arr2) {
      console.log(arr1, arr2);
      return arr1.filter((item) => {
        return arr2.includes(item);
      });
    },
    // 数组差集
    difference(arr1, arr2) {
      console.log(arr1, arr2);
      if (arr2.length > arr1.length) {
        return arr2.filter((v) => !arr1.includes(v));
      } else {
        return arr1.filter((v) => !arr2.includes(v));
      }
    },
    // 当面向对象 更改时 需要判断班级是否已经被使用
    classesChange(e, index) {
      this.curTypeIndex = index;
    },
    /* 获取面向对象 */
    getClass() {
        json1api(this.$store.getters.schoolId).then((res) => {
          let list = [];
          res.data.map((item) => {
            item.id = item.branch_id;
            item.name = item.branch_name;
            item.children = item.majorList;
            delete item.majorList;
            item.children.map((item2) => {
              item2.id = item2.major_id;
              item2.name = item2.major_name;
              item2.children = item2.classList;
              delete item.classList;
              item2.children.map((item3) => {
                item3.id = item3.class_id;
                item3.name = item3.class_name;
                list.push(item3);
              });
            });
          });
          console.log(list);
          this.classes = res.data;
          this.resourseClasses = list;
        });
      },
  },
};
export default mixin;
